/**
 * 2011-08-11 角色操作并联
 * huhui
 */

import com.Module.organization.Organization;
import com.Module.prms.operation;
import com.Module.prms.role;
import com.Module.prms.roleoption;

import common.checkbox.MycheckBox;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.Label;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import spark.components.HGroup;
import spark.components.Label;
import spark.components.VGroup;

private function initApp():void
{
	rolels.textInput.editable=false;
	orgsAction.findById(index.orgid);
	
}


/*
private function faultHandler(event:FaultEvent):void{
	Alert.show("出错："+event.fault.faultString);
}
*/


private function listHandler(event:ResultEvent):void{
	var page:Object =  event.result as Object;
	
	listData = page.rows as ArrayCollection;
}
private function orgidsHandler(event:ResultEvent):void{
	var org : Organization= event.result as Organization;
	var Pid:String = org.pid;
	var roleobj:role =  new role();
	roleoptrmobj.listmenuopt(Pid);
	roleobj.orgPid=Pid;
	roleRemoteobject.list(roleobj,0,10000);
}


private function listmenuoptHandler(event:ResultEvent):void{
	listmenuopt = event.result as ArrayCollection;
	cretecheckbox();

}
/**huhui 2011-08-11
 * 动态实例化模块，菜单，操作checkbox
 */

public function cretecheckbox()
{
	var pmsize:int=listmenuopt.length;
	for(var i:int=0;i<pmsize;i++)
	{
		var obj:Object = listmenuopt.getItemAt(i);
		var pmlable:spark.components.Label =new spark.components.Label();
		pmlable.text=obj.cmenuName;
		pmlable.setStyle("fontSize","16");
		pmlable.setStyle("fontWeight","bold");
		pmlable.setStyle("color","red");
			
		var pmHgroup:VGroup = new VGroup();
		pmHgroup.percentWidth=100;
		pmHgroup.percentHeight=20;
		pmHgroup.paddingLeft=30;
		pmHgroup.addElement(pmlable);
		
		var pmcarrc:ArrayCollection = obj.children;
		if(pmcarrc!=null)
		{

			var pmcsize:int=pmcarrc.length;
			for(var j:int=0;j<pmcsize;j++)
			{
				var objc:Object=pmcarrc.getItemAt(j);
				var menuopt:HGroup = new HGroup();
				var cmlabe:spark.components.Label = new spark.components.Label();
				cmlabe.text=objc.menuname;
				var cmlabelCanve:spark.components.BorderContainer = new spark.components.BorderContainer();
				cmlabelCanve.height=20;
				cmlabelCanve.width=200;
				cmlabelCanve.setStyle("borderVisible",false);
				cmlabelCanve.addElement(cmlabe);
//				var optComboxCanve:spark.components.BorderContainer = new spark.components.BorderContainer();
//				optComboxCanve.height=20;
//				optComboxCanve.width=800;
//				optComboxCanve.setStyle("borderVisible",false);
				menuopt.addElement(cmlabelCanve);
				var optarrc:ArrayCollection =objc.children;
				if(optarrc!=null)
				{
					var optcsize:int=optarrc.length;
					for(var k:int=0;k<optcsize;k++)
					{
						var opt:Object=optarrc.getItemAt(k);
						var optcheckbox:MycheckBox = new MycheckBox();
						
						optcheckbox.label=opt.coperationName;
						optcheckbox.id = opt.coperationId;
						optcheckbox.value=opt;
						optcheckbox.addEventListener(MouseEvent.CLICK,getoptsel);
						menuopt.addElement(optcheckbox);
						optmap.put(opt.coperationId,optcheckbox);/** 把操作对象放入hashmap */
						optid.push(opt.coperationId);/** 把操作对象id放入Array */
					}
				}
				//menuopt.addElement(optComboxCanve);
				pmHgroup.addElement(menuopt);
			}
		}
		mo_view.addElement(pmHgroup);
		
	}
	
}


public function getoptobj():void
{
	
	//testopt=optmap.getItem("4028850931ac970a0131ac97622c0002") as MycheckBox;
	//testopt.selected=true;
}
public function getoptsel(evt:MouseEvent):void
{
	var opt:MycheckBox = evt.currentTarget as MycheckBox;
	var obj:Object =opt.value;
	optmap.getItem(obj.coperationId).selected = opt.selected;
}


public function save():void
{
	if(rolels.selectedIndex==-1)
	{
		Alert.show("请选择角色！","提示");
		return;
	}
	var selopt:ArrayCollection = new ArrayCollection();
	var roleid:String = rolels.selectedItem.croleId.toString();
	for each (var item:String in optid) {
		var opttem:MycheckBox=optmap.getItem(item) as MycheckBox;
		if(opttem.selected)
		{
			var roleopt:roleoption=new roleoption();
			roleopt.cmenuId=(opttem.value).cmenuId;
			roleopt.coperationId =(opttem.value).coperationId;
			roleopt.croleId=roleid;
			//Alert.show(opttem.label);
			selopt.addItem(roleopt);
		}
	}
	if(selopt.length==0)
	{
		var roleopt:roleoption=new roleoption();
		roleopt.croleId=roleid;
		selopt.addItem(roleopt);
	}
	roleoptrmobj.save(selopt,roleid);
	
}

public function saveHandler(event:ResultEvent)
{
	var state:Boolean=event.result.state;
	var msg:String = event.result.msg;
	Alert.show(msg,"提示");
}


/**先把选中状态清空，然后通过数据库数据动态选定 */
public function getroleoptHander(event:ResultEvent)
{
	roleopt = event.result as ArrayCollection;
	
	for each(var optitemid:String in optid)
	{
		optmap.getItem(optitemid).selected=false;
	}

	if(roleopt!=null)
	{
		
		for each(var obj:Object in roleopt)
		{
			//Alert.show(optmap.size().toString());
			
			//optmap.getItem(obj.coperationId)
			if(optmap.getItem(obj.coperationId)!=null)
			{
				optmap.getItem(obj.coperationId).selected=true;
			}
		}
	}
}


